package com.controller;



import com.alibaba.fastjson.JSONObject;
import com.entity.StoreHouse;
import com.entity.Team;
import com.entity.User;
import com.entity.base.Result;

import com.service.StoreHouseService;
import com.service.TeamService;
import com.service.oss.OSSClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping(value = "/teacher")
@Slf4j
@Api(tags = "教师相关接口", description = "提供相关的 Rest API")
public class TeacherController {

    @Autowired
    OSSClient ossClient;

    @Autowired
    TeamService teamService;

    @Autowired
    StoreHouseService storeHouseService;


    @ApiOperation("创建资源库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "team", value = "班级实体", required = true, dataType = "user", paramType = "body")
    })
    @PostMapping("/createStoreHouse")
    public Result createStoreHouse(@RequestBody Team team){
        try {
            StoreHouse storeHouse = new StoreHouse();
            storeHouse.setId(System.currentTimeMillis());
            storeHouse.setStatus(1);
            storeHouse.setRoleId(1);
            storeHouse.setTeamId(team.getId());
            ossClient.mkDir("user_space/"+storeHouse.getId().toString()+"/");
            if(storeHouseService.save(storeHouse)) return Result.success(storeHouse);
            return Result.error("创建失败");
        }catch (Exception e){
            log.error(e.toString());
            return Result.error(e.toString());
        }
    }


    @ApiOperation("添加班级成员")
    @PostMapping("/addMemberToTeam")
    public Result addMemberToTeam(@RequestBody JSONObject jsonObject){
        return Result.success(teamService.addMember(Long.valueOf(jsonObject.get("teamId").toString()),Long.valueOf(jsonObject.get("userId").toString())));
    }

    @ApiOperation("批量添加班级成员")
    @PostMapping("/addMembersToTeam")
    public Result addMembersToTeam(@RequestBody JSONObject jsonObject){
        List<User> userList = (List<User>) jsonObject.get("userList");
        return Result.success(teamService.addMembers(Long.valueOf(jsonObject.get("teamId").toString()),userList));
    }

    @ApiOperation("删除班级成员")
    @PostMapping("/delMember")
    public Result delMember(@RequestBody JSONObject jsonObject){
        return Result.success(teamService.delMember(Long.valueOf(jsonObject.get("userId").toString())));
    }

    @ApiOperation("批量删除班级成员")
    @PostMapping("/delMembers")
    public Result delMembers(@RequestBody JSONObject jsonObject){
        List<User> userList = (List<User>) jsonObject.get("userList");
        return Result.success(teamService.delMembers(Long.valueOf(jsonObject.get("teamId").toString()),userList));
    }
}
